/**
 * Project Name:fastPlat
 * File Name:GroupController.java
 * Package Name:com.fast.cms.web.controller.admin
 * Date:2014年12月17日下午3:02:40
 * Copyright (c) 2014, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fast.base.cache.MemcachedUtils;
import com.fast.base.model.AnoSysLog;
import com.fast.base.util.LogUtils;
import com.fast.base.util.data.MathUtils;
import com.fast.base.util.web.ResponseData;
import com.fast.base.util.web.WebUtil;
import com.fast.base.web.BaseController;
import com.fast.common.CacheConstant;
import com.fast.common.userCache.AdminCacheUtils;
import com.fast.user.model.admin.AdminUser;
import com.fast.user.model.admin.Group;
import com.fast.user.service.admin.GroupService;

/**
 * ClassName:GroupController <br/>
 * 用途: 角色业务控制类 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  
 * @since    JDK 1.6
 * @see
 * Date:     2014年12月17日 下午3:02:40 <br/> 	 
 */
@Controller
@RequestMapping("/group")
public class GroupController extends BaseController{
	
	
	@Resource(name="groupService")
	private GroupService groupService;
	
	/**
	 * 核查角色名是否存在,主要用于添加用户时应用
	 * @param userName 用户名
	 * @param request 请求对象
	 * @param response 响应对象
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/checkGroupName.do",method=RequestMethod.GET)
	public String checkGroupName(String groupName){
		return Boolean.toString(!this.groupService.isExistGroupName(groupName));
	}
	
	/**
	 * 标题：updateGroupName <br/>
	 * 作用： 修改权限   <br/>
	 * @author developer-lushuifa
	 * @param  uuid
	 * @param  permissionArray
	 * @return
	 * @throws IOException 
	 * @since  JDK 1.6
	 */
	@ResponseBody
	@RequestMapping("/getComboboxData.do")
	public List<Group> getComboboxData() {
		return this.groupService.getGroupList();
	}
	
	/**
	 * 标题：updateGroupName <br/>
	 * 作用： 修改权限   <br/>
	 * @author developer-lushuifa
	 * @param uuid
	 * @param permissionArray
	 * @return
	 * @since JDK 1.6
	 */
	@ResponseBody
	@RequestMapping("/updateGroupName.do")
	@AnoSysLog(actionName="编辑角色信息")
	public ResponseData updateGroupName(String uuid,String groupName) {
		LogUtils.info("updateGroupName groupName:"+groupName);
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = groupService.updateGroupNameByUUID(groupName,uuid);
			if(!actionFlag){
				responseData.setMsg("更改角色名称失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("更改权限失败");
		}
		return responseData;
	}
	
	/**
	 * 标题：savePms2Group <br/>
	 * 作用： 修改权限内容    <br/>
	 * @author developer-lushuifa
	 * @param uuid 传入的UUID
	 * @param permissionArray 传入的权限字符串
	 * @return
	 * @since JDK 1.6
	 */
	@ResponseBody
	@RequestMapping("/savePms2Group.do")
	@AnoSysLog(actionName="保存权限信息到角色里")
	public ResponseData savePms2Group(String uuid,String permissionArray) {
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = groupService.updatePermissionArrayByUUID(permissionArray,uuid);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				//清除缓存
				MemcachedUtils.delete(CacheConstant.PERMISSION+"*");
				responseData.setMsg("更改权限成功");
			}else{
				responseData.setMsg("更改权限失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("更改权限失败");
		}
		return responseData;
	}

	/**
	 * 编辑[角色]信息
	 * @param formData
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editGroup.do")
	@AnoSysLog(actionName="编辑角色信息")
	public ResponseData editGroup(String formData) {
		Group group = JSON.parseObject(formData.replace("&quot;", "\'"),Group.class);
		AdminUser curAdminUser = AdminCacheUtils.getLoginAdminUser();
		ResponseData responseData = new ResponseData();
		try {
			//当存在UUID时的处理
			if(group.getUuid().equals("")){
				group.setUuid(MathUtils.getTheOnlyStr());
				group.setCreater(curAdminUser.getUserName());
				group.setCreaterId(curAdminUser.getId());
				group.setCreateTime(new Date());
			}
			group.setEditorId(curAdminUser.getId());
			group.setEditorName(curAdminUser.getUserName());
			group.setEditTime(new Date());
			boolean actionFlag = groupService.saveOrModifyGroup(group);
			if(actionFlag){
				//清除缓存
				MemcachedUtils.delete(CacheConstant.PERMISSION+"*");
				//返回数据给客户端
				responseData.setMsg("编辑[角色]["+group.getGroupName()+"]操作成功");
			}
			responseData.setCode(actionFlag?"00":"01");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMsg("编辑[角色]["+group.getGroupName()+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除[角色]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delGroup.do")
	@AnoSysLog(actionName="删除角色")
	public ResponseData delGroup(String uuid) {
		ResponseData responseData = new ResponseData();
		try {
			boolean actionFlag = groupService.deleteByUUID(Group.class,uuid);
			responseData.setCode(actionFlag?"00":"01");
			if(actionFlag){
				responseData.setMsg("删除角色编号为["+uuid+"]操作成功");
			}else{
				responseData.setMsg("删除角色编号为["+uuid+"]操作失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除角色编号为["+uuid+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 删除多个[[角色]]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delMoreGroup.do")
	@AnoSysLog(actionName="删除多个[角色]信息")
	public ResponseData delMoreGroup(String ids) {
		ResponseData responseData = new ResponseData();
		try {
			int actionCount = groupService.deleteByIds(Group.class, ids);
			responseData.setCode("00");
			responseData.setMsg("删除[角色]编号为["+ids+"]操作成功,共成功["+actionCount+"]");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("删除[角色]["+ids+"]操作失败");
		}
		return responseData;
	}
	
	/**
	 * 得到[角色]信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getJsonDetail.do")
	public ResponseData getGroupDetail(int id) {
		ResponseData responseData = new ResponseData();
		Group bean = groupService.queryForBeanById(Group.class, id);
		try {
			responseData.setCode("00");
			responseData.setData(bean);
			responseData.setMsg("获取信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setCode("01");
			responseData.setMsg("获取信息失败");
		}
		return responseData;
	}
	
	/**
	 * 删除[角色]信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getGroupDetail.do")
	public String getGroupDetail(int id,HttpServletRequest request,HttpServletResponse response) {
		WebUtil wu = new WebUtil(request,response);
		Group bean = groupService.queryForBeanById(Group.class, id);
		try {
			wu.setAttr("Group", bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/admin/admin/GroupDetail";
	}
	
	/**
	 * 标题：getCompanyList4Tree <br/>
	 * 作用： 得到单位列表以树型结构展示    <br/>
	 * @author developer-lushuifa
	 * @throws IOException 
	 * @since JDK 1.6
	 */
	@ResponseBody
	@RequestMapping("/getGroupList4Tree.do")
	public List<Map<String,Object>> getGroupList4Tree() throws IOException{
		List<Group> groupList = this.groupService.getGroupList();
		//父节点
		JSONObject parentNode = new JSONObject();
		parentNode.put("id", "0");
		parentNode.put("text","角色列表");
		parentNode.put("state","open");
		parentNode.put("iconCls","icon-folderPage");
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonOBject = null;
		if(null!=groupList&&groupList.size()>0){
			for (Group group : groupList) {
			    jsonOBject = new JSONObject();
			    jsonOBject.put("id", group.getUuid());
				jsonOBject.put("text",group.getGroupName());
				jsonOBject.put("iconCls","icon-page");
				//添加其它属性
				jsonOBject.put("attributes", null);
				jsonArray.add(jsonOBject);
			}
		}
		parentNode.put("children", jsonArray);
		List<Map<String,Object>> objectList = new ArrayList<Map<String,Object>>();
		objectList.add(parentNode);
		//return "["+parentNode.toString()+"]";
		return objectList;
	}
}

